<?php

namespace App\Http\Controllers\Api;

use App\Model\SendAllUserNotify;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class NotifyUserController extends BaseController
{
    protected $model; //全员推送消息模型

    public function __construct()
    {
    
        $this->model = new SendAllUserNotify(); //实例化模型[全员推送表]
    }
    /**
     * 添加全员推送
     */
    public function insertOrUpNotify(Request $request)
    {
        $param = $request->all();
        if(empty($param['notify_type'])){
            return $this->toJson(self::ERROR, [], '请选择推送消息类型');
        }
        if(!isset($param['is_all']))
        {
            return $this->toJson(self::ERROR, [], '请选择上传用户类型');
        }
        //参数is_all为0时候是非全员发送需要判断是否有上传用户id文件或者id字符串
        if(empty($param['uploadfile']) && empty($param['userids']) && $param['is_all']==0)
        {
            return $this->toJson(self::ERROR, [], '用户id任选其一,不能为空');
        }
        if(empty($param['sendtime']))
        {
            return $this->toJson(self::ERROR, [], '发送时间不能为空');
        }
        //图文消息上传
        if( isset($param['send_type']) && $param['send_type'] == 1 )
        {
            if(empty($param['img_url']))
            {
                return $this->toJson(self::ERROR, [], '图片不能为空');
            }
        }else{
            if(empty($param['send_content']))
            {
                return $this->toJson(self::ERROR, [], '发送文案不能为空');
            }
        }
        $userIds = [];
        //若非全员时候
        if( $param['is_all']==0)
        {
            if (!empty($param['uploadfile']))
            {
                //先将Excel读取入库后分页查询
                $fileurl = build_image( '/'.$param['uploadfile']);
                //下载文件
                $filepath = storage_path().'/'.'userlist.xls';
                file_put_contents($filepath, file_get_contents($fileurl));
                //读取Excel信息
                $array = excel_to_array($filepath, 1, '', 1);
    
                if (!empty($array['content'])) {
                    foreach ($array['content'] as $k => $v) {
                        $v=array_filter($v);
                        $userArr[] = $v;
                    }
                    $ExcelArr = array_reduce($userArr,'array_merge',[]);
                }
            }
            //整合
            if(!empty($param['userids']) && isset($ExcelArr))
            {
                $userIds = explode(',',$param['userids']);
                $userIds = array_merge($ExcelArr,$userIds);
            }else if(empty($param['userids']) && isset($ExcelArr)){
                $userIds = $ExcelArr;
            }else{
                $userIds = explode(',',$param['userids']);
            } 
        }
        $param['sendtime'] = strtotime(date("Y-m-d H:i",strtotime($param['sendtime'])));
        $param['userids'] = $userIds;
        //入库
        
        $res = $this->model->insertOrUpNotify($param);
        
        
        return $this->toJson(self::SUCCESS, $res);
    }
    /**
     * 全员推送消息列表
     */
    public function getnotifyList(Request $request)
    {
        $page = $request->get('page',1);//当前页
        $limit = $request->get('limit',10);//每页条数
        $send_status = $request->get('send_status',null);//发送状态
        $start_time = $request->get('start_time','');//开始时间
        $end_time = $request->get('end_time','');//结束时间
        $notify_type = $request->get('notify_type',0);//消息类型1=官方2=游戏官方
        //数据获取
        $data = $this->model->getnotifyList($page,$limit,$send_status,$start_time,$end_time,$notify_type);
        if(!empty($data))
        {
            foreach($data['data'] as $k=>$v)
            {
                $data['data'][$k]['send_time'] = date("Y-m-d H:i:s",$v['send_time']);
            }
        }
        return $this->toJson(self::SUCCESS, $data);
    }
    /**
     * 软删除推送
     */
    public function deleteNotify(Request $request)
    {
        $notifyid = $request->get('notifyid',0);//推送id
        if(empty($notifyid))
        {
            return $this->toJson(self::ERROR, [], '缺少id');
        }
        $changestatus = $request->get('status',1);//删除状态1删除0恢复
        //调用接口删除
        $data = $this->model->deleteNotify($notifyid,$changestatus);
        return $this->toJson(self::SUCCESS, $data);
    }
}


